package com.taiji.dicom4cloud.smart.alarm.handler;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.taiji.dicom4cloud.common.core.constrant.CommonConstants;
import com.taiji.dicom4cloud.common.core.utils.CommonUtils;
import com.taiji.dicom4cloud.smart.alarm.annoation.HandlerType;
import com.taiji.dicom4cloud.smart.alarm.common.EnumHandler;
import com.taiji.dicom4cloud.smart.alarm.common.MessageHandler;
import com.taiji.dicom4cloud.smart.alarm.model.Message;
import com.taiji.dicom4cloud.smart.alarm.utils.MalignantUtils;
import com.taiji.dicom4cloud.smart.alarm.utils.MessageUtils;
import com.taiji.dicom4cloud.smart.model.entity.AlarmEquipment;
import com.taiji.dicom4cloud.smart.model.entity.RfidRecord;
import com.taiji.dicom4cloud.smart.rfid.config.MqttGatewayService;
import com.taiji.dicom4cloud.smart.service.AlarmEquipmentService;
import com.taiji.dicom4cloud.smart.service.AlarmRecordService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;

/**
 * 恶性警报
 * @Description
 * @Author fanxl
 * @Date 2021/8/23
 **/
@HandlerType(value = EnumHandler.MALIGNANT_ALARM)
public class MalignantAlarmHandler implements MessageHandler {
    @Autowired
    private AlarmEquipmentService alarmEquipmentService;
    @Autowired
    private AlarmRecordService alarmRecordService;
    @Autowired
    private MqttGatewayService mqttGatewayService;

    @Override
    public Object handle(Message message) {
        AlarmEquipment alarmEquipment = alarmEquipmentService.getByEquipmentId(message.getEquipmentId());
        if(ObjectUtil.isNotNull(alarmEquipment)){
            alarmEquipment.setIsNormal(CommonConstants.IsNormal.ERROR);
            alarmEquipment.setIsAlarm(CommonConstants.IsAlarm.ALARM);
            alarmEquipment.setReportTime(LocalDateTime.now());
            alarmEquipmentService.updateById(alarmEquipment);


            RfidRecord record = new RfidRecord();
            BeanUtil.copyProperties(alarmEquipment,record);
            record.setEquipmentName(alarmEquipment.getAlarmEquipmentName());
            BeanUtil.copyProperties(message,record);
            record.setCreateTime(LocalDateTime.now());
            byte type0[] = {0x00};
            byte type1[] = {0x01};
            byte type2[] = {0x02};
            if(MessageUtils.compere(MalignantUtils.getType(message.getData()),type0)){
                Short type = 0;
                record.setAlarmType(type);
                record.setAlarmName(MalignantUtils.getAlarmName(message.getData()));
                record.setIsAlarm(CommonConstants.IsAlarm.ALARM);
                alarmRecordService.saveRecord(record);
                mqttGatewayService.send2Mqtt("alarm.rfid", JSONObject.toJSONString(record));
            }
            if(MessageUtils.compere(MalignantUtils.getType(message.getData()),type1)){
                Short type = 1;
                record.setAlarmType(type);
                record.setAlarmName(MalignantUtils.getAlarmName(message.getData()));
                record.setIsAlarm(CommonConstants.IsAlarm.ALARM);
                alarmRecordService.saveRecord(record);
                mqttGatewayService.send2Mqtt("alarm.rfid", JSONObject.toJSONString(record));
            }
            if(MessageUtils.compere(MalignantUtils.getType(message.getData()),type2)){
                Short type = 2;
                record.setAlarmType(type);
                record.setAlarmName(MalignantUtils.getAlarmName(message.getData()));
                record.setIsAlarm(CommonConstants.IsAlarm.ALARM);
                alarmRecordService.saveRecord(record);
                mqttGatewayService.send2Mqtt("alarm.rfid", JSONObject.toJSONString(record));
            }

        }
        return "MalignantAlarmHandler";
    }
}
